package me.hgj.mvvmhelper.widget.state;

import android.app.Activity;
import android.app.Dialog;
import android.os.Build;
import android.os.Bundle;
import android.text.Editable;
import android.text.InputFilter;
import android.text.TextWatcher;
import android.view.Gravity;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.RequiresApi;

import me.hgj.mvvmhelper.R;

public class InputWidgetDialog extends Dialog {
    protected Activity activity;
    private TextView sureBtn, cancelBtn, title;
    private EditText inputView;
    private DialogClickListener clickListener;
    private ImageView clearImg;

    public InputWidgetDialog(Activity activity) {
        super(activity, R.style.widgetDialogStyleRoundBg);
        this.activity = activity;
    }

    @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout_input_widget_dialog);
        initData();
    }


    protected void initData() {
        sureBtn = findViewById(R.id.btn_sure);
        cancelBtn = findViewById(R.id.btn_cancel);
        title = findViewById(R.id.dialog_title);
        inputView = findViewById(R.id.input_view);
        clearImg = findViewById(R.id.clear_img);

        setCanceledOnTouchOutside(true);

        getWindow().setGravity(Gravity.CENTER);
        setCanceledOnTouchOutside(false);

        cancelBtn.setOnClickListener(v -> {
            if (null != clickListener) {
                closeKeyBord();
                clickListener.onClick(1);
            }
        });


        sureBtn.setOnClickListener(v -> {
            if (null != clickListener) {
                closeKeyBord();
                clickListener.onClick(2);
            }
        });
        clearImg.setOnClickListener(new ClickProxy(v -> inputView.setText("")));
        setOnCancelListener(dialog -> InputUtil.closeKeybord(activity,inputView));
        inputView.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (inputView.getText().toString().replace(" ", "").length() == 0) {
                    clearImg.setVisibility(View.GONE);
                } else {
                    clearImg.setVisibility(View.VISIBLE);
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
    }

    public void setDialogTitle(String titleStr) {
        title.setText(null == titleStr ? "" : titleStr);
    }

    public void setInputHint(String inputHint) {
        inputView.setHint(null == inputHint ? "" : inputHint);
    }

    public void setDefaultInput(String inputHint) {
        inputView.setText(null == inputHint ? "" : inputHint);
        inputView.setSelection(inputView.getText().length());
    }

    public String getInputContent() {
        return inputView.getText().toString();
    }

    public void setClickListener(DialogClickListener clickListener) {
        this.clickListener = clickListener;
    }

    public void setInputMaxNumberStyle(int number) {

    }

    public void setInputLength(int length) {
        inputView.setFilters(new InputFilter[]{new InputFilter.LengthFilter(length)});
    }

    public void closeKeyBord() {
        InputUtil.closeKeybord(activity,inputView);
    }

    public interface DialogClickListener {
        /**
         * 1 取消  2 确定
         *
         * @param type
         */
        void onClick(int type);
    }

}
